import socket
import threading
import time

def fn1(server):
    count = 1
    while True:
        conn, raddr = server.accept() #blocking

        threading.Thread(target=fn2, args=(conn,), name="r{}".format(count), daemon=True).start()
        count += 1
def fn2(conn: socket.socket):
    try:
        for i in range(3):
            data = conn.recv(4096)
            if not data:
                print(conn.getpeername(), "bye~~~")
                return

            print(type(data), data)
            conn.send(data)
    except Exception as e:
        print(e)
    finally:
        conn.close()


if __name__ == '__main__':
    server = socket.socket()
    server.bind(('127.0.0.1',9999))
    server.listen(1024) #non-blocking

    threading.Thread(target=fn1, args=(server,), name="accept", daemon=True).start()

    while True:
        time.sleep(3)
        print([t.name for t in threading.enumerate()])


    #server.close()